package homework5;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

/**
 * Main Frame
 * 
 * @author eDisOn
 * 
 */
public class MainFrame extends JFrame
{
	private static Point location;
	private static Dimension size;

	private JLabel noticeLabel;
	private JTextArea inputArea;
	private JButton inputButton;
	private JPanel inputPanel;
	private JPanel showPanel;
	private JButton runButton;
	private JButton clearButton;
	private JPanel runPanel;
	private JTextArea nArea;
	private JTextArea vArea;
	private JTextArea wArea;
	private JTextArea resultArea;

	private int maximum;
	private int[] values;
	private int[] weights;

	static
	{
		int x = Toolkit.getDefaultToolkit().getScreenSize().width;
		int y = Toolkit.getDefaultToolkit().getScreenSize().height;

		size = new Dimension(x / 2, y / 2);
		location = new Point((x - size.width) / 2, (y - size.height) / 2);
	}

	public MainFrame()
	{
		super("Knapsack Problem (by@lianghao.lee)");
		init();
	}

	public void init()
	{
		showPanel = new JPanel();
		showPanel.setLayout(new GridLayout(12, 1));
		JLabel outputConsoleLabel = new JLabel(
				"------------------------------output Console-------------------------------");
		outputConsoleLabel.setFont(new Font("Garamond", Font.PLAIN, 24));
		showPanel.add(outputConsoleLabel);

		nArea = new JTextArea("Maximum Weight = ");
		nArea.setEditable(false);
		nArea.setBackground(Color.LIGHT_GRAY);
		nArea.setFont(new Font("Garamond", Font.PLAIN, 20));
		vArea = new JTextArea("Objects Values = ");
		vArea.setEditable(false);
		vArea.setBackground(Color.LIGHT_GRAY);
		vArea.setFont(new Font("Garamond", Font.PLAIN, 20));
		wArea = new JTextArea("Objects Weight = ");
		wArea.setEditable(false);
		wArea.setBackground(Color.LIGHT_GRAY);
		wArea.setFont(new Font("Garamond", Font.PLAIN, 20));
		resultArea = new JTextArea("Result = ");
		resultArea.setEditable(false);
		resultArea.setBackground(Color.LIGHT_GRAY);
		resultArea.setFont(new Font("Arial Black", Font.BOLD, 20));
		showPanel.add(nArea);
		showPanel.add(vArea);
		showPanel.add(wArea);
		showPanel.add(resultArea);

		JLabel inputConsoleLabel = new JLabel(
				"-------------------------------Input Console-------------------------------");
		inputConsoleLabel.setFont(new Font("Garamond", Font.PLAIN, 24));
		showPanel.add(inputConsoleLabel);

		noticeLabel = new JLabel("Maximum Weight:");
		noticeLabel.setFont(new Font("Garamond", Font.PLAIN, 20));
		inputArea = new JTextArea();
		inputArea.setBackground(Color.WHITE);
		inputArea.setFont(new Font("Garamond", Font.PLAIN, 20));
		inputButton = new JButton("OK");
		inputButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					if (noticeLabel.getText().equals("Maximum Weight:"))
					{
						maximum = Integer.parseInt(inputArea.getText().trim());
						nArea.setText(nArea.getText()
								+ inputArea.getText().trim());
						noticeLabel.setText("Objects Values[split by space]:");
						inputArea.setText("");
						return;
					}

					if (noticeLabel.getText().equals(
							"Objects Values[split by space]:"))
					{
						values = getValues(inputArea.getText());
						vArea.setText(vArea.getText() + "["
								+ inputArea.getText().trim() + "]");
						noticeLabel.setText("Objects Weights[split by space]:");
						inputArea.setText("");
						return;
					}

					if (noticeLabel.getText().equals(
							"Objects Weights[split by space]:"))
					{
						weights = getWeights(inputArea.getText());
						if (weights.length != values.length)
						{
							JOptionPane
									.showMessageDialog(
											null,
											"Values number is "
													+ values.length
													+ ", but Weights number is "
													+ weights.length
													+ "\nPlease input same number Values and Weights");
							inputArea.setText("");
						}
						else
						{
							wArea.setText(wArea.getText() + "["
									+ inputArea.getText().trim() + "]");
							noticeLabel
									.setText("Input Finished! Please Click \"Run\"");
							inputArea.setVisible(false);
							inputButton.setVisible(false);
							runButton.setEnabled(true);
						}
					}
				}
				catch (NumberFormatException nfe)
				{
					JOptionPane.showMessageDialog(null,
							"Error occur in input \nMessage: "
									+ nfe.getMessage());
					inputArea.setText("");
				}
			}
		});
		inputPanel = new JPanel();
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
		inputPanel.add(noticeLabel);
		inputPanel.add(inputArea);
		inputPanel.add(inputButton);
		showPanel.add(inputPanel);
		showPanel.add(new JLabel());

		add(showPanel, BorderLayout.CENTER);

		runButton = new JButton("Run");
		runButton.setEnabled(false);
		runButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				Knapsack knapsack = new Knapsack();
				int result = knapsack.knapsack(values.length, values, weights,
						maximum);
				resultArea.setText(resultArea.getText() + result);
				runButton.setEnabled(false);

			}
		});
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				inputArea.setVisible(true);
				inputButton.setVisible(true);

				// clear last info
				nArea.setText("Maximum Weight = ");
				vArea.setText("Objects Values = ");
				wArea.setText("Objects Weight = ");
				resultArea.setText("Result = ");
				noticeLabel.setText("Maximum Weight:");
				inputArea.setText("");
			}
		});

		runPanel = new JPanel();
		runPanel.setBackground(Color.LIGHT_GRAY);
		runPanel.add(runButton);
		runPanel.add(clearButton);
		add(runPanel, BorderLayout.SOUTH);

		setSize(size);
		setLocation(location);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false);
	}

	private int[] getValues(String input) throws NumberFormatException
	{
		StringTokenizer stk = new StringTokenizer(input);
		values = new int[stk.countTokens()];
		int i = 0;
		while (stk.hasMoreTokens())
		{
			values[i++] = Integer.parseInt(stk.nextToken());
		}
		return values;
	}

	private int[] getWeights(String input)
	{
		StringTokenizer stk = new StringTokenizer(input);
		weights = new int[stk.countTokens()];
		int i = 0;
		while (stk.hasMoreTokens())
		{
			weights[i++] = Integer.parseInt(stk.nextToken());
		}
		return weights;
	}

	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (UnsupportedLookAndFeelException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MainFrame frame = new MainFrame();
	}
}
